package com.atguigu.gulimall.order.interceptro;

import com.atguigu.common.constan.AuthServerConstant;
import com.atguigu.common.vo.MemberVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 拦截器
 */
@Component
public class OrderLoginInterceptro implements HandlerInterceptor{

    public static ThreadLocal<MemberVo> threadLocal = new ThreadLocal<>();
    //目标方法前拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //放行请求
        String requestURI = request.getRequestURI();
        boolean match = new AntPathMatcher().match("order/order/status/**", requestURI);
        if (match){
            return true;
        }
        HttpSession session = request.getSession();
        MemberVo attribute = (MemberVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute!=null){
            threadLocal.set(attribute);
            return true;
        }else {
            response.sendRedirect("http://localhost:20000/login.html");
            return true;
        }
    }
}


